from openai import OpenAI

while True:
    print("=============================")

    print("欢迎使用通义千问")

    question = input("请输入问题：")

    questions = '问题：' + question + "\n\n\n\n"

    print("保存问题中")

    with open("ty.txt", "a", encoding="utf-8") as f:
        f.write(questions)

    print("正在思考：1")


    try:
        client = OpenAI(
            # 若没有配置环境变量，请用阿里云百炼API Key将下行替换为：api_key="sk-xxx",
            # api_key=os.getenv("DASHSCOPE_API_KEY"),
            api_key = "sk-1b8bfcfb4e064536b93cc6e022c2e514",

            base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
        )

        print("正在思考：2")

        completion = client.chat.completions.create(
            model="qwen-max",
            # model="qwen-plus",  # 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
            messages=[
                {'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': question}
                ]
        )
        print(completion.choices[0].message.content)

        content = completion.choices[0].message.content

        newline = "\n\n\n\n\n\n"

        with open("ty.txt", "a", encoding="utf-8") as f:
            f.write(content + newline)



    except Exception as e:
        print(f"错误信息：{e}")
        print("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code")

    print('===============================')







#








# sk-1b8bfcfb4e064536b93cc6e022c2e514


